package com.ruoyi.xiche.mapper;

import com.ruoyi.xiche.domain.Store;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.ruoyi.xiche.domain.StoreWithRating;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

/**
* @author wangruoyang
* @description 针对表【store(门店表)】的数据库操作Mapper
* @createDate 2025-03-17 11:56:33
* @Entity com.ruoyi.xiche.domain.Store
*/
@Mapper
public interface StoreMapper extends BaseMapper<Store> {

    @Select("""
    SELECT
        s.store_id AS storeId,
        s.store_name AS storeName,
        s.business_license_image AS businessLicenseImage, 
        s.store_image AS storeImage, 
        COALESCE(AVG(sr.rating), 0) AS averageRating,
        s.business_hours AS businessHours,
        s.longitude,
        s.latitude,
        COUNT(o.order_id) AS orderCount
    FROM store s
    LEFT JOIN `order` o ON s.store_id = o.store_id
    LEFT JOIN service_review sr ON o.order_id = sr.order_id
    GROUP BY s.store_id, s.store_name, s.business_license_image, 
             s.store_image, s.business_hours, s.longitude, s.latitude;
""")
    List<Map<String, Object>> getStoreRatings();


    @Select("SELECT s.store_id, s.store_name, AVG(sr.rating) AS average_rating, " +
            "s.business_hours, s.longitude, s.latitude, " +
            "COUNT(o.order_id) AS order_count, " +
       "s.business_license_image, s.store_image " +
               "FROM store s " +
               "LEFT JOIN `order` o ON s.store_id = o.store_id " +
               "LEFT JOIN service_review sr ON o.order_id = sr.order_id " +
               "WHERE s.store_id = #{storeId} " +
               "GROUP BY s.store_id, s.store_name, s.business_hours, " +
               "s.longitude, s.latitude, s.business_license_image, s.store_image")
    List<StoreWithRating> getStoreInfoByStoreId(@Param("storeId") Long storeId);
}



